from tortoise import fields
from app.models.base import BaseModel

class Department(BaseModel):
    """部门模型"""
    name = fields.CharField(max_length=50, description="部门名称")
    parent_id = fields.IntField(null=True, description="父部门ID")
    sort = fields.IntField(default=0, description="排序")
    status = fields.BooleanField(default=True, description="状态：True-启用，False-禁用")
    
    users = fields.ReverseRelation["User"]
    
    class Meta:
        table = "departments"
        table_description = "部门表"
    
    def __str__(self):
        return self.name

class Permission(BaseModel):
    """权限模型"""
    name = fields.CharField(max_length=50, description="权限名称")
    code = fields.CharField(max_length=50, description="权限编码")
    type = fields.CharField(max_length=20, description="权限类型：menu-菜单，button-按钮")
    parent_id = fields.IntField(null=True, description="父权限ID")
    path = fields.CharField(max_length=100, null=True, description="路由路径")
    component = fields.CharField(max_length=100, null=True, description="组件路径")
    icon = fields.CharField(max_length=50, null=True, description="图标")
    sort = fields.IntField(default=0, description="排序")
    status = fields.BooleanField(default=True, description="状态：True-启用，False-禁用")
    
    class Meta:
        table = "permissions"
        table_description = "权限表"
    
    def __str__(self):
        return self.name

class Role(BaseModel):
    """角色模型"""
    name = fields.CharField(max_length=50, description="角色名称")
    code = fields.CharField(max_length=50, description="角色编码")
    description = fields.TextField(null=True, description="角色描述")
    status = fields.BooleanField(default=True, description="状态：True-启用，False-禁用")
    
    class Meta:
        table = "roles"
        table_description = "角色表"
    
    def __str__(self):
        return self.name

class User(BaseModel):
    """用户模型"""
    username = fields.CharField(max_length=50, unique=True, description="用户名")
    password = fields.CharField(max_length=128, description="密码")
    name = fields.CharField(max_length=50, description="姓名")
    email = fields.CharField(max_length=50, null=True, description="邮箱")
    phone = fields.CharField(max_length=20, null=True, description="手机号")
    avatar = fields.CharField(max_length=200, null=True, description="头像")
    status = fields.BooleanField(default=True, description="状态：True-启用，False-禁用")
    
    department = fields.ForeignKeyField("models.Department", related_name="users", null=True, description="部门")
    
    class Meta:
        table = "users"
        table_description = "用户表"
    
    def __str__(self):
        return self.username

class RolePermission(BaseModel):
    """角色权限关联表"""
    role = fields.ForeignKeyField("models.Role", related_name="role_permissions", description="角色")
    permission = fields.ForeignKeyField("models.Permission", related_name="permission_roles", description="权限")
    
    class Meta:
        table = "role_permissions"
        table_description = "角色权限关联表"
        unique_together = (("role", "permission"),)

class UserRole(BaseModel):
    """用户角色关联表"""
    user = fields.ForeignKeyField("models.User", related_name="user_roles", description="用户")
    role = fields.ForeignKeyField("models.Role", related_name="role_users", description="角色")
    
    class Meta:
        table = "user_roles"
        table_description = "用户角色关联表"
        unique_together = (("user", "role"),)